/*
 * Dijjer - A Peer to Peer HTTP Cache
 * Copyright (C) 2004,2005 Change.Tv, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package dijjer.io;

import java.net.MalformedURLException;
import java.net.URL;
import dijjer.io.comm.DMT;
import dijjer.io.comm.Message;
import dijjer.util.VeryLongIntegerKey;

/**
 * @author ian
 * 
 * To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code and
 * Comments
 * 
 * new URL(m.getString(DMT.URL)), m.getLong(DMT.FILE_LENGTH), m .getString(DMT.LAST_MODIFIED), m.getInt(DMT.CHUNK_NO),
 * 
 */
public class BlockInfo {

	URL _url;
	long _fileLength;
	String _lastModified;
	int _blockNo;
	VeryLongIntegerKey _hash, _hashHash;

	public BlockInfo(Message m) throws MalformedURLException {
		this(new URL(m.getString(DMT.URL)), m.getLong(DMT.FILE_LENGTH), m.getString(DMT.LAST_MODIFIED), m
				.getInt(DMT.BLOCK_NO));
	}

	public BlockInfo(URL url, long fileLength, String lastModified, int blockNo) {
		_url = url;
		_fileLength = fileLength;
		_lastModified = lastModified;
		_blockNo = blockNo;
	}

	public VeryLongIntegerKey getHashHashKey() {
		if (_hashHash == null) {
			_hashHash = new VeryLongIntegerKey(_url.toString() + ":" + _fileLength + ":" + _lastModified + ":" + _blockNo
					+ "-hash");
		}
		return _hashHash;
	}

	public VeryLongIntegerKey getHashKey() {
		if (_hash == null) {
			_hash = new VeryLongIntegerKey(_url.toString() + ":" + _fileLength + ":" + _lastModified + ":" + _blockNo);
		}
		return _hash;
	}

	public void setInMessage(Message m) {
		m.set(DMT.URL, _url.toString());
		m.set(DMT.FILE_LENGTH, _fileLength);
		m.set(DMT.LAST_MODIFIED, _lastModified);
		m.set(DMT.BLOCK_NO, _blockNo);
	}

	public int getBlockNo() {
		return _blockNo;
	}

	public long getFileLength() {
		return _fileLength;
	}

	public String getLastModified() {
		return _lastModified;
	}

	public URL getUrl() {
		return _url;
	}

	public String toString() {
		return _url.toString() + " (" + _fileLength + "|" + _lastModified + ")" + " " + _blockNo;
	}
}
